from typing import Annotated, Optional, List, Dict
from fastapi import Depends, FastAPI, HTTPException, Query
from sqlmodel import Field, Session, SQLModel, create_engine, select, Relationship
from datetime import datetime
from .systems import SystemOrg
from .scales import Scale

"""量表历史版本表

Keyword arguments:
ScaleHistory -- 量表历史版本表，存储量表历史版本信息
Return: return_description
"""


class ScaleHistory(SQLModel, table=True):
    __tablename__ = "scale_history"  # 表名
    __table_args__ = {"comment": "量表历史版本表，存储量表历史版本信息"}

    id: int | None = Field(default=None, primary_key=True, index=True)
    scale_id: int | None = Field(
        nullable=False, foreign_key="scale.id", ondelete="CASCADE"
    )  # 历史版本关联量表id
    detail_id: int | None = Field(
        nullable=False, foreign_key="scale_detail.id", ondelete="CASCADE"
    )  # 关联的量表详情id
    status: int = Field(
        nullable=False, default=0
    )  # 状态（0：草稿，1：已发布，2：已过期）

    # 关联的量表详情数据
    detail: "ScaleDetail" = Relationship(back_populates="scale_link")
    # 关联的量表数据
    scale: "Scale" = Relationship(back_populates="details_links")

    def __str__(self):
        return "ScaleHistory(scale_detail_id:{})".format(self.scale_id)


"""量表详情问题填空表

Keyword arguments:
ScaleDetailQuesBlankBase -- 量表详情问题填空基础模型
ScaleDetailQuesNextBlankBase -- 量表详情下一个问题填空基础模型
ScaleDetailQuesBlankCreate -- 量表详情问题填空创建模型
ScaleDetailQuesBlankUpdate -- 量表详情问题填空更新模型
ScaleDetailQuesBlankRead -- 量表详情问题填空读取模型
ScaleDetailQuesNextBlankRead  -- 量表详情下一个问题填空读取模型
ScaleDetailQuesBlank -- 量表详情问题填空表，存储量表详情问题填空信息
Return: return_description
"""


class ScaleDetailQuesBlankBase(SQLModel):
    order: str = Field(nullable=False, index=True, max_length=50)  # 填空顺序
    # 填空题类型（0：文本，1：数字，2：日期，3、手机号，4、日期，5、邮箱、6、地址、7、身高，8、体重,9、选择框)
    type: int = Field(nullable=False)
    placeholder: str | None = Field(max_length=500)  # 输入提示
    defaultValue: str | None = Field(max_length=500)  # 默认值
    # 校验规则类型（1：邮箱，2：数字，3：整数，4：时间，5：日期，6：电话号码，7：身份证号）
    validate_type: int | None = Field(nullable=True)
    # 是否计分
    is_score: bool = Field(default=False)


class ScaleDetailQuesNextBlankBase(SQLModel):
    order: str = Field(nullable=False, index=True, max_length=50)  # 填空顺序
    # 填空题类型（0：文本，1：数字，2：日期，3、手机号，4、日期，5、邮箱、6、地址、7、身高，8、体重,9、选择框)
    type: int = Field(nullable=False)
    placeholder: str | None = Field(max_length=500)  # 输入提示
    defaultValue: str | None = Field(max_length=500)  # 默认值
    # 校验规则类型（1：邮箱，2：数字，3：整数，4：时间，5：日期，6：电话号码，7：身份证号）
    validate_type: int | None = Field(nullable=True)
    value: str | None = Field(max_length=500, default=None)  # 填空值


class ScaleDetailQuesBlankCreate(ScaleDetailQuesBlankBase):
    pass


class ScaleDetailQuesBlankUpdate(ScaleDetailQuesBlankBase):
    id: int | None


class ScaleDetailQuesBlankRead(ScaleDetailQuesBlankBase):
    id: int | None


class ScaleDetailQuesNextBlankRead(ScaleDetailQuesNextBlankBase):
    id: int | None


class ScaleDetailQuesBlank(ScaleDetailQuesBlankBase, table=True):
    __tablename__ = "scale_detail_ques_blank"  # 表名
    __table_args__ = {"comment": "量表详情问题填空表，存储量表详情问题填空信息"}

    id: int | None = Field(default=None, primary_key=True, index=True)
    scale_detail_ques_id: int = Field(
        # 填空题选项对应的量表详情题目内容id
        nullable=False,
        foreign_key="scale_detail_ques.id",
        ondelete="CASCADE",
    )

    question: "ScaleDetailQues" = Relationship(
        back_populates="blanks")  # 关联的问题

    def __str__(self):
        return "ScaleDetailQuesBlank(id:{})".format(self.id)


"""量表详情问题多维单选行

Keyword arguments:
ScaleDetailQuesMatrixSingleOptionRowBase -- 量表详情问题多维单选行基础模型
ScaleDetailQuesMatrixSingleOptionRowCreate -- 量表详情问题多维单选行创建模型
ScaleDetailQuesMatrixSingleOptionRowUpdate -- 量表详情问题多维单选行更新模型
ScaleDetailQuesMatrixSingleOptionRowRead -- 量表详情问题多维单选行读取模型
ScaleDetailQuesMatrixSingleOptionRow -- 量表详情问题多维单选行表，存储详情问题多维单选行信息
Return: return_description
"""


class ScaleDetailQuesMatrixSingleOptionRowBase(SQLModel):
    order: str = Field(nullable=False, index=True, max_length=50)  # 行顺序
    classify: str | None = Field(max_length=2000)  # 行分类
    content: str = Field(max_length=2000)  # 行内容


class ScaleDetailQuesMatrixSingleOptionRowCreate(
    ScaleDetailQuesMatrixSingleOptionRowBase
):
    pass


class ScaleDetailQuesMatrixSingleOptionRowUpdate(
    ScaleDetailQuesMatrixSingleOptionRowBase
):
    id: int | None


class ScaleDetailQuesMatrixSingleOptionRowRead(
    ScaleDetailQuesMatrixSingleOptionRowBase
):
    id: int | None


class ScaleDetailNextQuesMatrixSingleOptionRowRead(
    ScaleDetailQuesMatrixSingleOptionRowBase
):
    id: int | None


class ScaleDetailQuesMatrixSingleOptionRow(
    ScaleDetailQuesMatrixSingleOptionRowBase, table=True
):
    __tablename__ = "scale_detail_ques_matrix_single_option_row"  # 表名
    __table_args__ = {"comment": "量表详情问题多维单选行表，存储详情问题多维单选行信息"}

    id: int | None = Field(default=None, primary_key=True, index=True)
    # 矩阵单题行选项对应的量表详情题目内容id
    scale_detail_ques_id: int = Field(
        nullable=False, foreign_key="scale_detail_ques.id", ondelete="CASCADE"
    )

    question: "ScaleDetailQues" = Relationship(back_populates="rows")  # 关联的问题

    def __str__(self):
        return "ScaleDetailQuesMatrixSingleOptionRow(id:{})".format(self.id)


"""量表详情问题多维单选列表

Keyword arguments:
ScaleDetailQuesMatrixSingleOptionColumnBase -- 量表详情问题多维单选列基础模型
ScaleDetailNextQuesMatrixSingleOptionColumnBase -- 量表详情问题多维单选列基础模型（用于关联下一题）
ScaleDetailQuesMatrixSingleOptionColumnCreate -- 量表详情问题多维单选列创建模型
ScaleDetailQuesMatrixSingleOptionColumnUpdate -- 量表详情问题多维单选列更新模型
ScaleDetailQuesMatrixSingleOptionColumnRead -- 量表详情问题多维单选列读取模型
ScaleDetailNextQuesMatrixSingleOptionColumnRead -- 量表详情问题多维单选列读取模型（用于关联下一题）
ScaleDetailQuesMatrixSingleOptionColumn -- 量表详情问题多维单选列表，存储详情问题多维单选列信息
Return: return_description
"""


class ScaleDetailQuesMatrixSingleOptionColumnBase(SQLModel):
    order: str = Field(nullable=False, index=True, max_length=50)  # 列顺序
    classify: str | None = Field(max_length=2000)  # 列的分类（支持多维）
    content: str = Field(max_length=2000)  # 列内容
    score: int | None = Field(nullable=True)  # 选项分数（如果分数为-1，则不计分）


class ScaleDetailNextQuesMatrixSingleOptionColumnBase(SQLModel):
    order: str = Field(nullable=False, index=True, max_length=50)  # 列顺序
    classify: str | None = Field(max_length=2000)  # 列的分类（支持多维）
    content: str = Field(max_length=2000)  # 列内容
    score: int | None = Field(nullable=True)  # 选项分数（如果分数为-1，则不计分）
    checked: int = Field(nullable=False, default=False)  # 是否选中（0：未选中，1：选中）


class ScaleDetailQuesMatrixSingleOptionColumnCreate(
    ScaleDetailQuesMatrixSingleOptionColumnBase
):
    pass


class ScaleDetailQuesMatrixSingleOptionColumnUpdate(
    ScaleDetailQuesMatrixSingleOptionColumnBase
):
    id: int | None


class ScaleDetailQuesMatrixSingleOptionColumnRead(
    ScaleDetailQuesMatrixSingleOptionColumnBase
):
    id: int | None


class ScaleDetailNextQuesMatrixSingleOptionColumnRead(
    ScaleDetailNextQuesMatrixSingleOptionColumnBase
):
    id: int | None


class ScaleDetailQuesMatrixSingleOptionColumn(
    ScaleDetailQuesMatrixSingleOptionColumnBase, table=True
):
    __tablename__ = "scale_detail_ques_matrix_single_option_column"  # 表名
    __table_args__ = {"comment": "量表详情问题多维单选列表，存储详情问题多维单选列信息"}

    id: int | None = Field(default=None, primary_key=True, index=True)
    # 矩阵单题列选项对应的量表详情题目内容id
    scale_detail_ques_id: int = Field(
        nullable=False, foreign_key="scale_detail_ques.id", ondelete="CASCADE"
    )

    question: "ScaleDetailQues" = Relationship(
        back_populates="columns")  # 关联的问题

    def __str__(self):
        return "ScaleDetailQuesMatrixSingleOptionColumn(id:{})".format(self.id)


"""量表详情问题选项表

Keyword arguments:
ScaleDetailQuesOptionBase -- 量表详情问题选项基础模型
ScaleDetailNextQuesOptionBase -- 量表详情问题选项下一题基础模型
ScaleDetailQuesOptionCreate -- 量表详情问题选项创建模型
ScaleDetailQuesOptionUpdate -- 量表详情问题选项更新模型
ScaleDetailQuesOptionRead -- 量表详情问题选项读取模型
ScaleDetailNextQuesOptionRead -- 量表详情问题选项下一题读取模型
ScaleDetailQuesOption -- 量表详情问题选项表，存储量表详情问题选项信息
Return: return_description
"""


class ScaleDetailQuesOptionBase(SQLModel):
    parent_id: int | None = Field(
        # 父选项id（如果是子选项，则需要填写父选项id）
        nullable=True,
        foreign_key="scale_detail_ques_option.id",
        ondelete="CASCADE",
    )
    order: str = Field(nullable=False, index=True, max_length=50)  # 选项顺序
    content: str = Field(max_length=2000, nullable=False)  # 选项内容
    # 选项后的输入框的类型，0：无，1：文本，2：数字，3：日期，4、手机号，5、日期，6、邮箱、7、地址、8、身高，9、体重
    type: int | None = Field(nullable=True)
    placeholder: str | None = Field(max_length=500)  # 输入提示
    defaultValue: str | None = Field(max_length=500)  # 默认值
    # 校验规则类型（1：邮箱，2：数字，3：整数，4：时间，5：日期，6：电话号码，7：身份证号）
    validate_type: int | None = Field(nullable=True)
    required: bool = Field(nullable=False)  # 选项后的输入框内容是否必填
    score: int | None = Field(nullable=True)  # 选项分数（如果分数为-1，则不计分）


class ScaleDetailNextQuesOptionBase(SQLModel):
    parent_id: int | None = Field(
        # 父选项id（如果是子选项，则需要填写父选项id）
        nullable=True,
        foreign_key="scale_detail_ques_option.id",
        ondelete="CASCADE",
    )
    order: str = Field(nullable=False, index=True, max_length=50)  # 选项顺序
    content: str = Field(max_length=2000, nullable=False)  # 选项内容
    # 选项后的输入框的类型，0：无，1：文本，2：数字，3：日期，4、手机号，5、日期，6、邮箱、7、地址、8、身高，9、体重
    type: int | None = Field(nullable=True)
    placeholder: str | None = Field(max_length=500)  # 输入提示
    defaultValue: str | None = Field(max_length=500)  # 默认值
    # 校验规则类型（1：邮箱，2：数字，3：整数，4：时间，5：日期，6：电话号码，7：身份证号）
    validate_type: int | None = Field(nullable=True)
    required: bool = Field(nullable=False)  # 选项后的输入框内容是否必填
    score: int | None = Field(nullable=True)  # 选项分数（如果分数为-1，则不计分）
    checked: int = Field(nullable=False, default=False) # 是否选中（0：未选中，1：选中）
    # 选项后的输入框内容
    value: str | None = Field(max_length=500, default=None)  # 输入提示


class ScaleDetailQuesOptionCreate(ScaleDetailQuesOptionBase):
    # 关联的问题列表
    option_ques_links: List["ScaleDetailOptionQuesLinkCreate"] | None = None


class ScaleDetailQuesOptionUpdate(ScaleDetailQuesOptionBase):
    id: int | None = Field(default=None, primary_key=True, index=True)
    option_ques_links: List[int] | None = None  # 关联的问题列表
    children: List["ScaleDetailQuesOptionUpdate"] | None = None  # 关联的子选项列表


class ScaleDetailQuesOptionRead(ScaleDetailQuesOptionBase):
    id: int | None = Field(default=None, primary_key=True, index=True)
    option_ques_links: List[int] | None = None  # 关联的问题列表
    children: List["ScaleDetailQuesOptionRead"] | None = None  # 关联的子选项列表


class ScaleDetailNextQuesOptionRead(ScaleDetailNextQuesOptionBase):
    id: int | None = Field(default=None, primary_key=True, index=True)
    children: List["ScaleDetailNextQuesOptionRead"] | None = None  # 关联的子选项列表


class ScaleDetailQuesOption(ScaleDetailQuesOptionBase, table=True):
    __tablename__ = "scale_detail_ques_option"  # 表名
    __table_args__ = {"comment": "量表详情问题选项表，存储量表详情问题选项信息"}

    id: int | None = Field(default=None, primary_key=True, index=True)
    scale_detail_ques_id: int = Field(
        nullable=False, foreign_key="scale_detail_ques.id", ondelete="CASCADE"
    )  # 选项对应的量表详情题目内容id

    question: "ScaleDetailQues" = Relationship(
        back_populates="options")  # 关联的问题
    option_ques_links: List["ScaleDetailOptionQuesLink"] = Relationship(
        back_populates="option"
    )  # 关联的问题选项列表（多对一）

    def __str__(self):
        return "ScaleDetailQuesOption(id:{})".format(self.id)


"""跳转逻辑列表

Keyword arguments:
ScaleJumpLogicBase -- 跳转逻辑基础模型
ScaleJumpLogicCreate -- 跳转逻辑创建模型
ScaleJumpLogicUpdate -- 跳转逻辑更新模型
ScaleJumpLogicRead -- 跳转逻辑读取模型
ScaleJumpLogic -- 跳转逻辑模型
Return: return_description
"""


class ScaleJumpLogicBase(SQLModel):
    ques_id: int = Field(
        nullable=False, foreign_key="scale_detail_ques.id", ondelete="CASCADE"
    )  # 跳转逻辑关联问题id
    next_question_id: int | None  # 下一题的问题id（如果为空则跳转到结束）
    logic: int = Field(nullable=False)  # 逻辑（0：和，1：或）


class ScaleJumpLogicCreate(ScaleJumpLogicBase):
    jump_logic_details: List["ScaleJumpLogicDetailCreate"] | None = []


class ScaleJumpLogicUpdate(ScaleJumpLogicBase):
    id: int | None

    jump_logic_details: List["ScaleJumpLogicDetailRead"] | None = []


class ScaleJumpLogicRead(ScaleJumpLogicBase):
    id: int | None = Field(default=None, primary_key=True, index=True)

    jump_logic_details: List["ScaleJumpLogicDetailRead"] | None = []


class ScaleJumpLogic(ScaleJumpLogicBase, table=True):
    __tablename__ = "scale_jump_logic"  # 表名
    __table_args__ = {"comment": "量表跳转逻辑列表，存储量表跳转逻辑信息"}

    id: int | None = Field(default=None, primary_key=True, index=True)

    question: "ScaleDetailQues" = Relationship(
        back_populates="jump_logics",
    )  # 关联的问题
    jump_logic_details: List["ScaleJumpLogicDetail"] = Relationship(
        back_populates="jump_logic",
    )  # 关联的跳转逻辑规则列表（多对一）

    def __str__(self):
        return "ScaleJumpLogic(id:{})".format(self.id)


"""跳转逻辑详情列表

Keyword arguments:
ScaleJumpLogicDetailBase -- 跳转逻辑详情基础模型
ScaleJumpLogicDetailCreate -- 跳转逻辑详情创建模型
ScaleJumpLogicDetailUpdate -- 跳转逻辑详情更新模型
ScaleJumpLogicDetailRead -- 跳转逻辑详情读取模型
ScaleJumpLogicDetail -- 跳转逻辑详情模型
Return: return_description
"""


class ScaleJumpLogicDetailBase(SQLModel):
    jump_logic_id: int = Field(
        nullable=False, foreign_key="scale_jump_logic.id", ondelete="CASCADE"
    )  # 关联的跳转逻辑id
    ques_id: int = Field(
        nullable=False, foreign_key="scale_detail_ques.id", ondelete="CASCADE"
    )  # 跳转逻辑关联id
    option_id: int | None = Field(
        nullable=True, foreign_key="scale_detail_ques_option.id", ondelete="CASCADE"
    )  # 跳转逻辑关联选项id
    line_id: int | None = Field(
        nullable=True,
        foreign_key="scale_detail_ques_matrix_single_option_row.id",
        ondelete="CASCADE",
    )  # 跳转逻辑关联选项行id
    row_id: int | None = Field(
        nullable=True,
        foreign_key="scale_detail_ques_matrix_single_option_column.id",
        ondelete="CASCADE",
    )  # 跳转逻辑关联选项列id
    logic: int = Field(nullable=False)  # 逻辑（1：等于，0：不等于）


class ScaleJumpLogicDetailCreate(ScaleJumpLogicDetailBase):
    pass


class ScaleJumpLogicDetailUpdate(ScaleJumpLogicDetailBase):
    id: int | None


class ScaleJumpLogicDetailRead(ScaleJumpLogicDetailBase):
    id: int | None


class ScaleJumpLogicDetail(ScaleJumpLogicDetailBase, table=True):
    __tablename__ = "scale_jump_logic_detail"  # 表名
    __table_args__ = {
        "comment": "量表跳转逻辑规则详情列表，存储量表跳转逻辑规则详情信息"
    }

    id: int | None = Field(default=None, primary_key=True, index=True)
    

    jump_logic: "ScaleJumpLogic" = Relationship(
        back_populates="jump_logic_details"
    )  # 关联的跳转逻辑（多对一）

    def __str__(self):
        return "ScaleJumpLogicDetail(id:{})".format(self.id)


"""量表选项关联题目表

Keyword arguments:
ScaleDetailOptionQuesLink -- 量表选项关联题目模型
Return: return_description
"""


class ScaleDetailOptionQuesLinkBase(SQLModel):
    ques_id: int = Field(
        nullable=False, foreign_key="scale_detail_ques.id", ondelete="CASCADE"
    )  # 关联的问题id


class ScaleDetailOptionQuesLinkCreate(ScaleDetailOptionQuesLinkBase):
    pass


class ScaleDetailOptionQuesLinkUpdate(ScaleDetailOptionQuesLinkBase):
    id: int | None
    option_id: int | None


class ScaleDetailOptionQuesLink(ScaleDetailOptionQuesLinkBase, table=True):
    __tablename__ = "scale_detail_option_ques_link"  # 表名
    __table_args__ = {
        "comment": "量表详情问题选项关联题目表，存储量表详情问题选项关联题目信息"
    }

    id: int | None = Field(default=None, primary_key=True, index=True)
    option_id: int = Field(
        nullable=False, foreign_key="scale_detail_ques_option.id", ondelete="CASCADE"
    )  # 关联的选项id

    option: "ScaleDetailQuesOption" = Relationship(
        back_populates="option_ques_links"
    )  # 关联的选项（多对一）

    def __str__(self):
        return "ScaleDetailQuesOptionLink(id:{})".format(self.id)

"""量表详情问题表

Keyword arguments:
ScaleDetailQuesBase -- 量表详情问题基础模型
ScaleDetailNextQuesBase -- 量表详情问题跳转基础模型
ScaleDetailQuesCreate -- 量表详情问题创建模型
ScaleDetailQuesUpdate -- 量表详情问题更新模型
ScaleDetailQuesRead -- 量表详情问题读取模型
ScaleDetailNextQuesRead -- 量表详情问题跳转读取模型
ScaleDetailQues -- 量表详情问题模型
Return: return_description
"""


class ScaleDetailQuesBase(SQLModel):
    order: str = Field(nullable=False, index=True, max_length=50)  # 题目顺序编号（唯一）
    # 题目类型（1：单选题，2：多选题，3：单项填空，4：多项填空、5、矩阵单选、6、多维矩阵单选、7、基础量表）
    type: int = Field(nullable=False)
    title: str | None = Field(nullable=True, max_length=250)  # 题目内容
    hassubtitle: bool | None = Field(nullable=True, default=None)  # 是否有子标题
    subtitle: str | None = Field(nullable=True, max_length=5000)  # 子标题
    required: bool | None = Field(nullable=False, default=True)  # 是否必填
    basic_scale_id: int | None = Field(nullable=True)  # 基础量表id
    # 答题逻辑（1：关联题目逻辑，2：题目跳转逻辑）
    response_logic: int | None = Field(nullable=True, default=None)


class ScaleDetailNextQuesBase(SQLModel):
    order: str = Field(nullable=False, index=True, max_length=50)  # 题目顺序编号（唯一）
    # 题目类型（1：单选题，2：多选题，3：单项填空，4：多项填空、5、矩阵单选、6、多维矩阵单选、7、基础量表）
    type: int = Field(nullable=False)
    title: str | None = Field(nullable=True, max_length=250)  # 题目内容
    hassubtitle: bool | None = Field(nullable=True, default=None)  # 是否有子标题
    subtitle: str | None = Field(nullable=True, max_length=5000)  # 子标题
    required: bool | None = Field(nullable=False, default=True)  # 是否必填


class ScaleDetailQuesCreate(ScaleDetailQuesBase):
    id: int | None = None
    options: List[ScaleDetailQuesOptionCreate] | None = []  # 题目关联选项列表
    blanks: List[ScaleDetailQuesBlankCreate] | None = []  # 题目关联填空列表
    # 题目关联多维单选行列表
    rows: List[ScaleDetailQuesMatrixSingleOptionRowCreate] | None = []
    # 题目关联多维单选列列表
    columns: List[ScaleDetailQuesMatrixSingleOptionColumnCreate] | None = []
    jump_logics: List[ScaleJumpLogicCreate] | None = []  # 题目关联跳转逻辑列表


class ScaleDetailQuesUpdate(ScaleDetailQuesBase):
    id: int | None = None
    options: List[ScaleDetailQuesOptionUpdate] | None = []  # 题目关联选项列表
    blanks: List[ScaleDetailQuesBlankUpdate] | None = []  # 题目关联填空列表
    # 题目关联多维单选行列表
    rows: List[ScaleDetailQuesMatrixSingleOptionRowUpdate] | None = []
    # 题目关联多维单选列列表
    columns: List[ScaleDetailQuesMatrixSingleOptionColumnUpdate] | None = []
    jump_logics: List[ScaleJumpLogicUpdate] | None = []  # 题目关联跳转逻辑列表


class ScaleDetailQuesRead(ScaleDetailQuesBase):
    id: int | None = None
    options: List["ScaleDetailQuesOptionRead"] | None = []  # 题目关联选项列表
    blanks: List["ScaleDetailQuesBlankRead"] | None = []  # 题目关联填空列表
    rows: List["ScaleDetailQuesMatrixSingleOptionRowRead"] = (
        []
    )  # 题目关联多维单选行列表
    # 题目关联多维单选列列表
    columns: List["ScaleDetailQuesMatrixSingleOptionColumnRead"] = []
    jump_logics: List["ScaleJumpLogicRead"] | None = []  # 题目关联跳转逻辑列表


class ScaleDetailNextQuesRead(ScaleDetailNextQuesBase):
    id: int | None = None
    is_first: bool = Field(default=False)  # 是否是第一题
    is_last: bool = Field(default=False)  # 是否是最后最后一题

    options: List["ScaleDetailNextQuesOptionRead"] | None = []  # 题目关联选项列表
    blanks: List["ScaleDetailQuesNextBlankRead"] | None = []  # 题目关联填空列表
    rows: List["ScaleDetailNextQuesMatrixSingleOptionRowRead"] = (
        []
    )  # 题目关联多维单选行列表
    # 题目关联多维单选列列表
    columns: List["ScaleDetailNextQuesMatrixSingleOptionColumnRead"] = []


class ScaleDetailQues(ScaleDetailQuesBase, table=True):
    __tablename__ = "scale_detail_ques"  # 表名
    __table_args__ = {"comment": "量表详情题目表，存储量表详情题目信息"}

    id: int = Field(default=None, primary_key=True, index=True)
    scale_detail_id: int = Field(
        nullable=False, foreign_key="scale_detail.id", ondelete="CASCADE"
    )  # 量表详情题目对应的id

    scale_detail: "ScaleDetail" = Relationship(
        back_populates="questions"
    )  # 关联的量表详情（多对一）
    options: List["ScaleDetailQuesOption"] = Relationship(
        back_populates="question"
    )  # 选项关联题目列表（反向查询）
    blanks: List["ScaleDetailQuesBlank"] = Relationship(
        back_populates="question"
    )  # 填空关联题目列表（反向查询）
    rows: List["ScaleDetailQuesMatrixSingleOptionRow"] = Relationship(
        back_populates="question"
    )  # 多维单选行关联题目列表（反向查询）
    columns: List["ScaleDetailQuesMatrixSingleOptionColumn"] = Relationship(
        back_populates="question"
    )  # 多维单选列关联题目列表（反向查询）
    jump_logics: List["ScaleJumpLogic"] = Relationship(
        back_populates="question"
    )  # 跳转逻辑关联题目列表（反向查询）

    def __str__(self):
        return "ScaleDetailQues(id:{})".format(self.id)


"""量表详情表

Keyword arguments:
ScaleDetailBase -- 量表详情基础模型
ScaleDetailCreate -- 新增量表详情模型
ScaleDetailUpdate -- 编辑量表详情模型
ScaleDetailRead -- 量表详情读取模型
RespJsonScaleDetail -- 响应体模型
RespJsonScaleDetailQues -- 响应体模型(获取下一题)
ScaleDetail -- 量表详情模型（表）
Return: return_description
"""


class ScaleDetailBase(SQLModel):
    scale_id: int = Field(
        nullable=False, foreign_key="scale.id", ondelete="CASCADE"
    )  # 量表详情关联量表id
    name: str = Field(max_length=50, nullable=False)  # 量表名称
    description: str | None = Field(default=None, max_length=1000)  # 量表描述


class ScaleDetailCreate(ScaleDetailBase):
    id: int | None = Field(default=None, primary_key=True, index=True)
    questions: List[ScaleDetailQuesCreate] | None = []  # 问题列表


class ScaleDetailUpdate(ScaleDetailBase):
    id: int | None = Field(default=None, primary_key=True, index=True)
    questions: List[ScaleDetailQuesUpdate] | None = []  # 问题列表


class ScaleDetailRead(ScaleDetailBase):
    id: int | None = Field(default=None, primary_key=True, index=True)
    questions: List[ScaleDetailQuesRead] | None = []  # 问题列表


class RespJsonScaleDetail(SQLModel):
    code: int = Field(default=200)
    msg: str = Field(default="查询成功")
    data: ScaleDetailRead | List = []


class RespJsonScaleDetailQues(SQLModel):
    code: int = Field(default=200)
    msg: str = Field(default="查询成功")
    data: ScaleDetailNextQuesRead | None = {}


class ScaleDetail(ScaleDetailBase, table=True):
    __tablename__ = "scale_detail"  # 表名
    __table_args__ = {"comment": "量表详情表，存储量表详情信息"}

    id: int | None = Field(default=None, primary_key=True, index=True)

    scale_link: "ScaleHistory" = Relationship(
        back_populates="detail"
    )  # 关联的量表（反向查询）
    questions: List["ScaleDetailQues"] = Relationship(
        back_populates="scale_detail"
    )  # 问题列表
    # 量表评分规则
    scale_score_rules: List["ScaleScoreRule"] = Relationship(
        back_populates="scale_detail"
    )

    def __str__(self):
        return "ScaleDetail(name:{})".format(self.name)


"""量表评分计算规则列表

Keyword arguments:
ScaleScoreRuleBase -- 量表评分计算规则列表基类
ScaleScoreRuleCreate -- 量表评分计算规则列表创建类
ScaleScoreRuleUpdate -- 量表评分计算规则列表更新类
ScaleScoreRuleRead -- 量表评分计算规则列表读取类
RespJsonScaleScoreRule -- 量表评分计算规则列表响应体
ScaleScoreRule -- 量表评分计算规则列表
Return: return_description
"""


class ScaleScoreRuleBase(SQLModel):
    algorithm: int = Field(
        nullable=False
    )  # 计算方式（1：T分，2：维度T分，3：多维T分，4：自定义规则）


class ScaleScoreRuleCreate(ScaleScoreRuleBase):
    scale_detail_id: int | None = None

    logics: List["ScaleScoreRuleLogicCreate"] = []
    dimensions: List["ScaleScoreRuleDimensionCreate"] = []
    custom_rules: List["ScaleScoreRuleCustomRuleCreate"] = []


class ScaleScoreRuleUpdate(ScaleScoreRuleBase):
    id: int | None = None
    scale_detail_id: int | None = None

    logics: List["ScaleScoreRuleLogicUpdate"] = []
    dimensions: List["ScaleScoreRuleDimensionUpdate"] = []
    custom_rules: List["ScaleScoreRuleCustomRuleUpdate"] = []


class ScaleScoreRuleRead(ScaleScoreRuleBase):
    id: int
    scale_detail_id: int | None = None

    logics: List["ScaleScoreRuleLogicRead"] = []
    dimensions: List["ScaleScoreRuleDimensionRead"] = []
    custom_rules: List["ScaleScoreRuleCustomRuleRead"] = []


class RespJsonScaleScoreRule(SQLModel):
    code: int = Field(default=200)
    msg: str = Field(default="查询成功")
    data: List[ScaleScoreRuleRead] = []


class ScaleScoreRule(ScaleScoreRuleBase, table=True):
    __tablename__ = "scale_score_rule"  # 表名
    __table_args__ = {"comment": "量表评分计算规则列表，存储量表评分计算规则信息"}

    id: int | None = Field(default=None, primary_key=True, index=True)
    scale_detail_id: int | None = Field(
        nullable=False, foreign_key="scale_detail.id", ondelete="CASCADE"
    )  # 关联的量表id

    scale_detail: "ScaleDetail" = Relationship(
        back_populates="scale_score_rules"
    )  # 关联的量表详情

    logics: List["ScaleScoreRuleLogic"] = Relationship(
        back_populates="rule"
    )  # 关联的量表评分计算规则逻辑列表
    dimensions: List["ScaleScoreRuleDimension"] = Relationship(
        back_populates="rule"
    )  # 关联的量表评分计算规则维度列表
    custom_rules: List["ScaleScoreRuleCustomRule"] = Relationship(
        back_populates="rule"
    )  # 关联的量表评分计算规则自定义规则列表

    def __str__(self):
        return "ScaleScoreRule(id:{})".format(self.id)


"""量表评分规则关联维度列表

Keyword arguments:
ScaleScoreRuleDimensionBase -- 量表评分规则关联维度列表基类
ScaleScoreRuleDimensionCreate -- 量表评分规则关联维度列表创建类
ScaleScoreRuleDimensionUpdate -- 量表评分规则关联维度列表更新类
ScaleScoreRuleDimensionRead -- 量表评分规则关联维度列表读取类
ScaleScoreRuleDimension -- 量表评分规则关联维度列表
Return: return_description
"""


class ScaleScoreRuleDimensionBase(SQLModel):
    name: str = Field(max_length=50, nullable=False)  # 维度名称
    order: str = Field(nullable=False, index=True, max_length=50)  # 维度顺序


class ScaleScoreRuleDimensionCreate(ScaleScoreRuleDimensionBase):
    # 关联的量表评分关联维度题目或者行列表
    dimensions_ques_links: List["ScaleScoreRuleDimensionQuesLinkCreate"] = []
    logics: List["ScaleScoreRuleLogicCreate"] = []


class ScaleScoreRuleDimensionUpdate(ScaleScoreRuleDimensionBase):
    id: int | None = None
    # 关联的量表评分关联维度题目或者行列表
    dimensions_ques_links: List["ScaleScoreRuleDimensionQuesLinkUpdate"] = []
    logics: List["ScaleScoreRuleLogicUpdate"] = []


class ScaleScoreRuleDimensionRead(ScaleScoreRuleDimensionBase):
    id: int | None = None
    # 关联的量表评分关联维度题目或者行列表
    dimensions_ques_links: List["ScaleScoreRuleDimensionQuesLinkUpdate"] = []
    logics: List["ScaleScoreRuleLogicRead"] = []


class ScaleScoreRuleDimension(ScaleScoreRuleDimensionBase, table=True):
    __tablename__ = "scale_score_rule_dimension"  # 表名
    __table_args__ = {"comment": "量表评分规则关联维度列表，存储量表评分关联维度信息"}
    id: int | None = Field(default=None, primary_key=True, index=True)
    scale_score_rule_id: int = Field(
        nullable=False, foreign_key="scale_score_rule.id", ondelete="CASCADE"
    )  # 关联的量表评分计算规则id

    rule: "ScaleScoreRule" = Relationship(
        back_populates="dimensions"
    )  # 关联的量表评分计算规则
    dimensions_ques_links: List["ScaleScoreRuleDimensionQuesLink"] = Relationship(
        back_populates="dimension"
    )  # 关联的量表评分关联维度题目或者行列表
    logics: List["ScaleScoreRuleLogic"] = Relationship(
        back_populates="dimension"
    )  # 关联的量表评分计算规则逻辑列表

    def __str__(self):
        return "ScaleScoreRuleDimension(id:{})".format(self.id)


"""量表评分规则自定义规则列表

Keyword arguments:
ScaleScoreRuleCustomRuleBase -- 量表评分规则自定义规则列表基类
ScaleScoreRuleCustomRuleCreate -- 量表评分规则自定义规则列表创建类
ScaleScoreRuleCustomRuleUpdate -- 量表评分规则自定义规则列表更新类
ScaleScoreRuleCustomRuleRead -- 量表评分规则自定义规则列表读取类
ScaleScoreRuleCustomRule -- 量表评分规则自定义规则列表
Return: return_description
"""


class ScaleScoreRuleCustomRuleBase(SQLModel):
    name: str = Field(max_length=50, nullable=False)  # 自定义规则名称
    order: str = Field(nullable=False, index=True, max_length=50)  # 自定义规则顺序
    expression: str = Field(max_length=500, nullable=False)  # 自定义规则表达式


class ScaleScoreRuleCustomRuleCreate(ScaleScoreRuleCustomRuleBase):
    logics: List["ScaleScoreRuleLogicUpdate"] = []


class ScaleScoreRuleCustomRuleUpdate(ScaleScoreRuleCustomRuleBase):
    id: int | None = None
    logics: List["ScaleScoreRuleLogicUpdate"] = []


class ScaleScoreRuleCustomRuleRead(ScaleScoreRuleCustomRuleBase):
    id: int | None = None
    logics: List["ScaleScoreRuleLogicRead"] = []


class ScaleScoreRuleCustomRule(ScaleScoreRuleCustomRuleBase, table=True):
    __tablename__ = "scale_score_rule_custom_rule"  # 表名
    __table_args__ = {
        "comment": "量表评分规则自定义规则列表，存储量表评分自定义规则信息"
    }
    id: int | None = Field(default=None, primary_key=True, index=True)
    scale_score_rule_id: int = Field(
        nullable=False, foreign_key="scale_score_rule.id", ondelete="CASCADE"
    )

    rule: "ScaleScoreRule" = Relationship(
        back_populates="custom_rules"
    )  # 关联的量表评分计算规则id
    logics: List["ScaleScoreRuleLogic"] = Relationship(
        back_populates="custom_rule"
    )  # 关联的量表评分计算规则逻辑列表

    def __str__(self):
        return "ScaleScoreRuleCustomRule(id:{})".format(self.id)


"""量表评分规则维度关联题目或者行列表

Keyword arguments:
ScaleScoreRuleDimensionQuesLinkBase -- 量表评分规则维度关联题目或者行列表基类
ScaleScoreRuleDimensionQuesLinkCreate -- 量表评分规则维度关联题目或者行列表创建类
ScaleScoreRuleDimensionQuesLinkUpdate -- 量表评分规则维度关联题目或者行列表更新类
ScaleScoreRuleDimensionQuesLinkRead -- 量表评分规则维度关联题目或者行列表读取类
ScaleScoreRuleDimensionQuesLink -- 量表评分规则维度关联题目或者行列表
Return: return_description

"""


class ScaleScoreRuleDimensionQuesLinkBase(SQLModel):
    ques_id: int = Field(
        nullable=False, foreign_key="scale_detail_ques.id", ondelete="CASCADE"
    )  # 关联的量表详情题目id
    row_id: int | None = Field(
        nullable=True,
        foreign_key="scale_detail_ques_matrix_single_option_row.id",
        ondelete="CASCADE",
    )  # 关联的量表详情题目行id


class ScaleScoreRuleDimensionQuesLinkCreate(ScaleScoreRuleDimensionQuesLinkBase):
    pass


class ScaleScoreRuleDimensionQuesLinkUpdate(ScaleScoreRuleDimensionQuesLinkBase):
    id: int | None = None


class ScaleScoreRuleDimensionQuesLinkRead(ScaleScoreRuleDimensionQuesLinkBase):
    id: int | None = None


class ScaleScoreRuleDimensionQuesLink(ScaleScoreRuleDimensionQuesLinkBase, table=True):
    __tablename__ = "scale_score_rule_dimension_ques_link"  # 表名
    __table_args__ = {
        "comment": "量表评分规则维度关联题目或者行列表，存储量表评分关联维度题目或者行信息"
    }

    id: int | None = Field(default=None, primary_key=True, index=True)
    dimension_id: int = Field(
        nullable=False, foreign_key="scale_score_rule_dimension.id", ondelete="CASCADE"
    )  # 关联的量表评分关联维度id

    dimension: "ScaleScoreRuleDimension" = Relationship(
        back_populates="dimensions_ques_links"
    )  # 关联的量表评分关联维度

    def __str__(self):
        return "ScaleScoreRuleDimensionQuesLink(id:{})".format(self.id)


"""量表评分计算关联问题逻辑列表

Keyword arguments:
ScaleScoreRuleLogicBase -- 量表评分计算关联问题逻辑列表基类
ScaleScoreRuleLogicCreate -- 量表评分计算关联问题逻辑列表创建类
ScaleScoreRuleLogicUpdate -- 量表评分计算关联问题逻辑列表更新类
ScaleScoreRuleLogicRead -- 量表评分计算关联问题逻辑列表读取类
ScaleScoreRuleLogic -- 量表评分计算关联问题逻辑列表
Return: return_description
"""


class ScaleScoreRuleLogicBase(SQLModel):
    classify: str | None = Field(nullable=True)  # 所属维度(列)
    start: int | None = Field(nullable=True, default=None)  # 开始分数
    start_include: bool = Field(nullable=False, default=True)  # 开始分数是否包含
    end: int | None = Field(nullable=True, default=None)  # 结束分数
    end_include: bool = Field(nullable=False, default=True)  # 结束分数是否包含
    description: str = Field(max_length=500)  # 评分描述


class ScaleScoreRuleLogicCreate(ScaleScoreRuleLogicBase):
    pass


class ScaleScoreRuleLogicUpdate(ScaleScoreRuleLogicBase):
    id: int | None = None


class ScaleScoreRuleLogicRead(ScaleScoreRuleLogicBase):
    id: int | None = None


class ScaleScoreRuleLogic(ScaleScoreRuleLogicBase, table=True):
    __tablename__ = "scale_score_rule_logic"  # 表名
    __table_args__ = {
        "comment": "量表评分计算关联问题逻辑列表，存储量表评分关联问题逻辑信息"
    }

    id: int | None = Field(default=None, primary_key=True, index=True)
    scale_score_rule_id: int | None = Field(
        nullable=True, foreign_key="scale_score_rule.id", ondelete="CASCADE"
    )  # 关联的量表评分计算规则id
    dimension_id: int | None = Field(
        nullable=True, foreign_key="scale_score_rule_dimension.id", ondelete="CASCADE"
    )  # 关联的量表评分计算规则维度id
    custom_rule_id: int | None = Field(
        nullable=True, foreign_key="scale_score_rule_custom_rule.id", ondelete="CASCADE"
    )  # 关联的量表评分计算规则自定义规则id

    rule: "ScaleScoreRule" = Relationship(
        back_populates="logics"
    )  # 关联的量表评分计算规则
    dimension: "ScaleScoreRuleDimension" = Relationship(
        back_populates="logics"
    )  # 关联的量表评分计算规则维度
    custom_rule: "ScaleScoreRuleCustomRule" = Relationship(
        back_populates="logics"
    )  # 关联的量表评分计算规则自定义规则

    def __str__(self):
        return "ScaleScoreRuleQuesLogic(id:{})".format(self.id)
